/**	*	set globals 
	*	register handler for on-load event
*** */
	var gadgetHelper = null;
	_IG_RegisterOnloadHandler( loadVisualizationAPI );
function loadVisualizationAPI() {
	google.load( "visualization", "1" );
	google.setOnLoadCallback( sendQuery );
}
function sendQuery() {
	var prefs = new _IG_Prefs();
	gadgetHelper = new google.visualization.GadgetHelper();
	var query = gadgetHelper.createQueryFromPrefs( prefs );
	query.send( handleQueryResponse );
} 
function handleQueryResponse( response ) {
	if ( !gadgetHelper.validateResponse( response ) )	{ return; }
	var tableData = response.getDataTable();
	var html = buildTopDivHTML( tableData );
	loadContainerDiv( 'topdiv', html );
	html = buildTableDivHTML ( tableData );
	loadContainerDiv( 'tablediv', html );
	// fire a request to dynamically adjust window height
	// The function is defined in the XML script
	// because the XML script has access to User Prefs defined there.
	adjustHeight();
} 
function buildTopDivHTML( tableData ) {
	var html = [];
	// could not access User Pref __UP_feedUrl__ from included code
	// so I moved the definition and initialization of feedburnerURL to the XML
	//var feedburnerURL = __UP_feedUrl__ ;
	html.push( '<a class="feed" href="' + feedburnerURL + '">Subscribe to comments</a>' );
	html.push( '<span class="counter">Comments' );
	var row = tableData.getNumberOfRows() - 1;
	var timestamp = tableData.getFormattedValue(row,0);
	timestamp = escapeSafeHtml( timestamp );
	if ( timestamp != 'Timestamp' ) { html.push( ' (' + (row+1) + ')' ); }
	html.push( '</span>' );
	return html;
}
function escapeSafeHtml(text) {
	if (text == null) { return ''; }
	return _hesc( text );
}
function buildTableDivHTML (tableData) {
	var html = [];
	var row = tableData.getNumberOfRows() - 1;
	for (row = 0; row != tableData.getNumberOfRows(); row++) {
		var timestamp = escapeSafeHtml( tableData.getFormattedValue(row,0) );
		if ( timestamp == 'Timestamp' )	{
			// spreadsheet header row value indicates that no data rows are present
			html.push( '<div class="no-comments">Be the first to start the discussion!</div>' );
		}
		else	{
			var commenterName = tableData.getFormattedValue( row,1 );
			var commenterURL = tableData.getFormattedValue( row,2 );
			var comment = tableData.getFormattedValue( row,3 );
			var commenterName = escapeSafeHtml( commenterName );
			var commenterURL = escapeSafeHtml( commenterURL );
			html.push ( '<div class="' + rowDivClass( commenterName ) + '">' );
			html.push ( '<div class="headers"><span class="numbers">No. ' + ( row+1 ) + '</span>' );
			html.push ( spanOrAnchor ( commenterName, commenterURL ) );
			html.push ( '<span class="timestamps">' + timestamp + '</span>' );
			html.push ( '</div><!-- close headers div -->' );
			html.push ( commentDiv ( comment ) );
			html.push ( '</div><!-- close row Div -->' );
		} 
	}
	return html;
}
function rowDivClass( commenterName ) {
	if (commenterName == adminDisplayName ) 
			{ return 'admin-comments' ; }
	else	{ return 'viewers-comments' ; }
}
function spanOrAnchor ( commenterName, commenterURL ) {
	if ( commenterURL == "" ) 
			{ return '<span class="names">' + commenterName + '</span>' ; }
	else	{ return '<a class="names" href="' + commenterURL + '">' + commenterName + '</a>' ; }
}
function commentDiv ( comment ) {
	// safely replace linefeed with <br>
	comment = replacer(comment, /\n/g, "jfdklsfjoiejfdklsfjoidsajfkl");
	comment = escapeSafeHtml(comment);
	comment = replacer(comment, /jfdklsfjoiejfdklsfjoidsajfkl/g, "<br>");
	return '<div class="comments">' + comment + '</div>' ;
}
function loadContainerDiv ( divName, html ) {
	var targetDiv = _gel( divName );
	targetDiv.innerHTML = html.join('');
}
function replacer( string, find, replace ) {
	return string.replace( find, replace );
}
